/* eslint-disable */
window.hkNativeCallBackList = {};

window.hkJSBridge = {
    call: hkiOSNativeCall,
    updateParent: updateParent,
};

String.prototype.hkNativehashCode = function () {
    let hash = 0;
    if (this.length == 0) return hash;
    for (let index = 0; index < this.length; index++) {
        let charactor = this.charCodeAt(index);
        hash = ((hash << 5) - hash) + charactor;
        hash = hash & hash;
    }
    return hash;
}


//console日志下发给native端(android端代码需要重新写，否则js报错，导致通道失效)
let console = {};
console.log = function (message) {
    //使用到webkit会导致android 报错
    window.webkit.messageHandlers['logger'].postMessage(message)
}


//异常捕获的信息量比较少，可以采用Raven.js，类似腾讯bugly
window.onerror = function (message, url, line, column, error) {
//  console.log('log---onerror::::',message, url, line, column, error);
    console.log('onerror');

    console.log(message);
    console.log(error);
    console.log(line);
}

function updateParent(message) {
    window.webkit.messageHandlers['updateParent'].postMessage(message)
}


/*
 identifier:
 接口唯一标识，js通过唯一标识匹配到需要调用的js函数


 resultStatus
 1: 返回数据，并关闭通道
 2：返回数据，不关闭通道
 3：只是关闭通道

 resultData
 native返回给js的json格式字符串

native端调用时，传递的3个参数都是字符串类型

 android调用时 该方法收到的resultData为jsonObject类型
 iOS调用时，该方法收到的resultData为json字符串
*/
window.HKAppCallback = function (identifier, resultStatus, resultData) {

    let callBackfunc = window.hkNativeCallBackList[identifier];

    if (callBackfunc) {
        console.log("resultStatus " + resultStatus);

        if (resultStatus < 3) {

            if (typeof resultData == 'string') {
                try {
                    resultData = JSON.parse(resultData);
                } catch (e) {
                    // console.log(e);
                    console.log("resultData parse to json fail：" + resultData);
                }
            }
            callBackfunc(resultData);
            // console.log("callBackfunc");
        }

        if (resultStatus != 2) {
            window.hkNativeCallBackList[identifier] = null;
            delete window.hkNativeCallBackList[identifier];
            // console.log("delete window.hkNativeCallBackList");
        }
    } else {
        // console.log("identifier:" + identifier + " not exist");
        return "identifier:" + identifier + " not exist"
    }
}

/*
iOS 调用native通道，iOS WKWebView需要注册消息名 WKNativeMethodMessage
    [configuration.userContentController addScriptMessageHandler:scriptMessageHandler name:@"WKNativeMethodMessage"];

*/
function hkiOSNativeCall(bridgeObjc) {
    let dataString = encodeURIComponent(JSON.stringify(bridgeObjc.data));
    let timestamp = Date.parse(new Date());
    let identifier = (bridgeObjc.target + bridgeObjc.action + dataString + timestamp).hkNativehashCode().toString();
    window.hkNativeCallBackList[identifier] = bridgeObjc.callback;
    window.webkit.messageHandlers.WKNativeMethodMessage.postMessage({
        targetName: bridgeObjc.target,
        actionName: bridgeObjc.action,
        data: bridgeObjc.data,
        identifier: identifier,
    });
}

/*

 //js -> android
function hkAndroidNativeCall(bridgeObjc) {
    let dataString = encodeURIComponent(JSON.stringify(bridgeObjc.data));
    let timestamp = Date.parse(new Date());
    let identifier = (bridgeObjc.target + bridgeObjc.action + dataString + timestamp).hkNativehashCode().toString();
    window.hkNativeCallBackList[identifier] = bridgeObjc.callback;

    //android参数必须是内置类型，string,int, jsonobject类型不支持
    let data = JSON.stringify(bridgeObjc.data);
    window.hkNativeAndroid.postMessage({
        bridgeObjc.target,
        bridgeObjc.action,
        data,
        identifier,
    });
}
*/


/*
native端 webview不使用的时候调用，比如复用缓存，当前页面结束的时候调用该方法
*/
window.hkNativeCallBackMethodManager = {
    removeAllCallBacks: function () {
        window.hkNativeCallBackList = {};
        console.log("hkNativeCallBackMethodManager removeAllCallBacks log");
    }
};
